import Vue from 'vue'
import VueRouter from 'vue-router'
import common from './modules/common' //公用模块
import democratic from './modules/democratic' // 民主测评
import board from './modules/board' // 纪实
import evalute from './modules/evalute' // 评价
import mertis from './modules/mertis' // 绩效
import notice from './modules/notice' // 消息，通知
import userManage from './modules/userManage' // 用户管理
import outduty from './modules/outduty' // 考勤管理
import bonus from './modules/bonus' // 加分项
import dailyManage from './modules/dailyManage' // 日常管理
import meet from './modules/meet' // 会议管理
import task from './modules/task' // 任务管理

import { Toast } from 'vant'

Vue.use(VueRouter)

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return routerPush.call(this, location, onResolve, onReject)
  return routerPush.call(this, location).catch(error => error)
}

const routes = [
  ...common, //公用模块
  ...democratic, //民主测评
  ...board, //纪实模块
  ...evalute, //评价模块
  ...mertis, //绩效模块
  ...notice, //消息通知
  ...userManage, //用户管理
  ...outduty, // 考勤管理
  ...bonus /* 加分项 */,
  ...dailyManage /* 日常管理 */,
  ...meet /* 会议管理 */,
  ...task, // 任务管理
]
const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

let first = null
router.beforeEach((to, from, next) => {
  if (config.production) {
    if ((from.path != '/' && to.path == '/login') || (from.name == 'Home' && to.name == 'nativeMessagein')) {
      if (!first) {
        first = new Date().getTime()
        Toast({
          message: '再点一次退出',
          position: 'bottom',
        })
        setTimeout(() => {
          first = null
        }, 1000)
      } else {
        if (new Date().getTime() - first < 1000) {
          //如果两次按下的时间小于1s，
          setTimeout(() => {
            localStorage.clear()
            sessionStorage.clear()
            window.flutter_inappwebview.callHandler('logOut', '')
            first = null
          }, 500)
        }
      }
      next(false)
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
